#include "CChatHandler.h"

#include "CChatSession.h"
#include "CChatServer.h"
void CChatHandler::add(CChatSession *session)
{
    session->push("Login Success");
    broadcast(session->name+" has enterd the room.\n");
    server->users[session->name]=session;
    CHandler::add(session);
}

void CChatHandler::remove(CChatSession *session)
{
    CHandler::remove(session);
    broadcast(session->name+" has left the room."+session->terminator);
}

void CChatHandler::handle(CChatSession *session, QByteArray line)
{
    if(line.isEmpty())return;
    QByteArrayList parts=line.split(' ');
    QByteArray cmd=parts[0];
    QByteArray content=parts[1];
    if(cmd.toLower().contains("say"))
    {
        broadcast(session->name+" "+cmd+" : "+content+session->terminator);
    }
    else if(cmd.toLower().contains("look"))
    {
        session->push("Online Users:"+session->terminator);
        for(auto other : sessions)
        {
            session->push(other->name+other->terminator);
        }
    }

}



